/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.course.authorization.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.zx.course.authorization.entity.TcourseAuthorization;
import com.jeesite.modules.zx.course.authorization.service.TcourseAuthorizationService;
import com.jeesite.modules.zx.course.entity.Tcourses;
import com.jeesite.modules.zx.course.service.TcoursesService;

/**
 * 用户课程授权管理Controller
 * @author zpp
 * @version 2020-02-29
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/course/authorization/tcourseAuthorization")
public class TcourseAuthorizationController extends BaseController {

	@Autowired
	private TcourseAuthorizationService tcourseAuthorizationService;
	@Autowired
	private TcoursesService tcoursesService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TcourseAuthorization get(String id, boolean isNewRecord) {
		return tcourseAuthorizationService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:course:authorization:tcourseAuthorization:view")
	@RequestMapping(value = {"list", ""})
	public String list(TcourseAuthorization tcourseAuthorization, Model model) {
		
		model.addAttribute("tcourseAuthorization", tcourseAuthorization);
		return "modules/zx/course/authorization/tcourseAuthorizationList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:course:authorization:tcourseAuthorization:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TcourseAuthorization> listData(TcourseAuthorization tcourseAuthorization, HttpServletRequest request, HttpServletResponse response) {
		tcourseAuthorization.setPage(new Page<>(request, response));
		Page<TcourseAuthorization> page = tcourseAuthorizationService.findPage(tcourseAuthorization);
	/*	for(TcourseAuthorization taz:page.getList()){
			String courseId = taz.getCourseId();
			Tcourses tcourses = tcoursesService.get(courseId);
			taz.setCourseName(tcourses.getCourseTitle());
		}*/
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:course:authorization:tcourseAuthorization:view")
	@RequestMapping(value = "form")
	public String form(TcourseAuthorization tcourseAuthorization, Model model) {
		Tcourses tcourses=new Tcourses();
		tcourses.setParentCode("0");
		List<Tcourses> tcourseList=tcoursesService.findList(tcourses);
		model.addAttribute("tcourseList", tcourseList);
		model.addAttribute("tcourseAuthorization", tcourseAuthorization);
		return "modules/zx/course/authorization/tcourseAuthorizationForm";
	}

	/**
	 * 保存用户课程授权管理
	 */
	@RequiresPermissions("zx:course:authorization:tcourseAuthorization:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TcourseAuthorization tcourseAuthorization) {
		tcourseAuthorizationService.save(tcourseAuthorization);
		return renderResult(Global.TRUE, text("保存用户课程授权管理成功！"));
	}
	
	/**
	 * 停用用户课程授权管理
	 */
	@RequiresPermissions("zx:course:authorization:tcourseAuthorization:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TcourseAuthorization tcourseAuthorization) {
		tcourseAuthorization.setStatus(TcourseAuthorization.STATUS_DISABLE);
		tcourseAuthorizationService.updateStatus(tcourseAuthorization);
		return renderResult(Global.TRUE, text("停用用户课程授权管理成功"));
	}
	
	/**
	 * 启用用户课程授权管理
	 */
	@RequiresPermissions("zx:course:authorization:tcourseAuthorization:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TcourseAuthorization tcourseAuthorization) {
		tcourseAuthorization.setStatus(TcourseAuthorization.STATUS_NORMAL);
		tcourseAuthorizationService.updateStatus(tcourseAuthorization);
		return renderResult(Global.TRUE, text("启用用户课程授权管理成功"));
	}
	
	/**
	 * 删除用户课程授权管理
	 */
	@RequiresPermissions("zx:course:authorization:tcourseAuthorization:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TcourseAuthorization tcourseAuthorization) {
		tcourseAuthorizationService.delete(tcourseAuthorization);
		return renderResult(Global.TRUE, text("删除用户课程授权管理成功！"));
	}
	
}